//
//  ModifyPasswordViewController.m
//  Router
//
//  Created by teki on 15/10/19.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "SettingSSIDAndPasswordViewController.h"
#import "CustomTextField.h"
#import "LoginResponseModel.h"
#import "WIFIHelper.h"
@interface SettingSSIDAndPasswordViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UIScrollView *rootScrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) CustomTextField *phoneTextField;
//@property (nonatomic, strong) CustomTextField *macAddressCodeTextField;
@property (nonatomic, strong) CustomTextField *passwordTextField;

@property (nonatomic, strong) UIButton *storeButton;


@property (nonatomic, strong) WIFIHelper *wifiHelper;
@end

@implementation SettingSSIDAndPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.wifiHelper = [[WIFIHelper alloc] init];
    
    self.title = @"设置WIFI";
    
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self initContentView];
    
    
    //WIFIModel *wifi = [UserSingleton shareInstance].userModel.wifi;
    //self.phoneTextField.textField.text = wifi.ssid;
    //self.macAddressCodeTextField.textField.text = wifi.device_id;
    
    [self.phoneTextField.textField becomeFirstResponder];
    // Do any additional setup after loading the view.
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)initContentView
{
    
    //内容跟视图
    _rootScrollView=[[UIScrollView alloc]init];
    _rootScrollView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:_rootScrollView];
    [_rootScrollView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureClick:)];
    [_rootScrollView addGestureRecognizer:tapGesture];
    //内容视图
    _contentView = UIView.new;
    _contentView.backgroundColor = [UIColor clearColor];
    [_rootScrollView addSubview:_contentView];
    [_contentView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_rootScrollView);
        make.width.equalTo(_rootScrollView);
    }];
    
    
    
//    UIFont *labelFont = [UIFont systemFontOfSize:15.0f];
//    UIColor*textColor = SeparatorLineColor;
    UIColor *borderColor = [UIColor colorWithHexString:@"c3c3c3"];
    int fontSize = 14.0f;
    
    CGFloat leadingPadding = 35;
    CGFloat labelPadding = 35;
    CGFloat textFieldPadding = 30;
    CGFloat textFieldHeight = 38.0f;
    
//    UILabel *moble = [_contentView addLabelWithText:@"设备名称:" font:labelFont color:textColor];
//    moble.textAlignment = NSTextAlignmentRight;
    _phoneTextField = [[CustomTextField alloc]initWithType:CustomTextFieldTypeNone target:self FontSize:fontSize];
    [_phoneTextField setStyleColor:borderColor];
    _phoneTextField.textField.textColor = [UIColor blackColor];
    _phoneTextField.placeholder = @"请设置WIFI的名称";
    _phoneTextField.textField.delegate = self;
    _phoneTextField.textField.returnKeyType = UIReturnKeyNext;
    [_contentView addSubview:_phoneTextField];
    
    
//    UILabel *macLabel = [_contentView addLabelWithText:@"MAC地址:" font:labelFont color:textColor];
//    macLabel.textAlignment = NSTextAlignmentRight;
//    _macAddressCodeTextField = [[CustomTextField alloc]initWithType:CustomTextFieldTypeNone target:self FontSize:fontSize];
//    [_macAddressCodeTextField setStyleColor:borderColor];
//    _macAddressCodeTextField.textField.textColor = [UIColor blackColor];
//    _macAddressCodeTextField.placeholder = @"请输入母猫MAC地址";
//    _macAddressCodeTextField.textField.returnKeyType = UIReturnKeyNext;
//    _macAddressCodeTextField.textField.delegate =self;
//    _macAddressCodeTextField.textField.textColor = [UIColor blackColor];
//    [_contentView addSubview:_macAddressCodeTextField];
    
    
    
    
//    UILabel *passwordLabel = [_contentView addLabelWithText:@"密    码:" font:labelFont color:textColor];
//    passwordLabel.textAlignment = NSTextAlignmentRight;
    _passwordTextField = [[CustomTextField alloc]initWithType:CustomTextFieldTypeNone target:self FontSize:fontSize];
    [_passwordTextField setStyleColor:borderColor];
    _passwordTextField.textField.secureTextEntry = YES;
    _passwordTextField.textField.textColor = [UIColor blackColor];
    _passwordTextField.placeholder = @"请设置WIFI的密码";
    _passwordTextField.isShowPasswordButton = YES;
    _passwordTextField.textField.returnKeyType = UIReturnKeyDone;
    _passwordTextField.textField.delegate =self;
    _passwordTextField.textField.textColor = [UIColor blackColor];
    
    [_contentView addSubview:_passwordTextField];
    
//    [moble makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(leadingPadding);
//        make.top.equalTo(32);
//        //        make.width.equalTo(50);
//        make.right.equalTo(macLabel.right);
//    }];
    
    [_phoneTextField makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(40);
        make.left.equalTo(self.view).offset(labelPadding);
        make.right.equalTo(_contentView.right).offset(-leadingPadding);
        make.height.equalTo(textFieldHeight);
    }];
    
    
//    [_macAddressCodeTextField makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(_phoneTextField.bottom).offset(textFieldPadding);
//        make.left.equalTo(self.view).offset(labelPadding);
//        make.right.equalTo(_contentView.right).offset(-leadingPadding);
//        make.height.equalTo(textFieldHeight);
//    }];
    
//    [macLabel makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(leadingPadding);
//        make.top.equalTo(moble.bottom).offset(40);
//        //        make.width.equalTo(50);
//        make.right.equalTo(moble.right);
//    }];
    
    
//    [passwordLabel makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(leadingPadding);
//        make.top.equalTo(macLabel.bottom).offset(40);
//        make.right.equalTo(macLabel.right);
//    }];
    
    
    [_passwordTextField makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_phoneTextField.bottom).offset(textFieldPadding);
        make.left.equalTo(self.view).offset(labelPadding);
        make.right.equalTo(_contentView.right).offset(-leadingPadding);
        make.height.equalTo(textFieldHeight);
    }];
    
    
    
    
    
    
    
    
    UIButton *finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    finishButton.translatesAutoresizingMaskIntoConstraints = NO;
    finishButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [finishButton setTitle:@"保  存" forState:UIControlStateNormal];
    finishButton.layer.cornerRadius = 5.0f;
    finishButton.layer.masksToBounds = YES;
    [finishButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [finishButton setBackgroundImage:[UIImage imageWithColor:NavgationBarBackgroundColor] forState:UIControlStateNormal];
    [finishButton setBackgroundImage:[UIImage imageWithColor:RGB(78, 148, 70)] forState:UIControlStateSelected];
    [finishButton addTarget:self action:@selector(finishButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:finishButton];
    [finishButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_passwordTextField.bottom).offset(textFieldPadding);
        make.left.equalTo(leadingPadding);
        make.right.equalTo(-leadingPadding);
        make.height.equalTo(45);
        make.bottom.equalTo(_contentView.bottom);
        
    }];
    
    
    
}




#pragma mark -
#pragma mark   UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([textField isEqual:_phoneTextField.textField]) {
//        [_macAddressCodeTextField.textField becomeFirstResponder];
//    }
//    else if ([textField isEqual:_macAddressCodeTextField.textField])
//    {
        [_passwordTextField.textField becomeFirstResponder];
        
    }
    else{
        [_passwordTextField.textField resignFirstResponder];
    }
    return YES;
}


#pragma mark -
#pragma mark   IBActions
- (void)tapGestureClick:(UITapGestureRecognizer *)guesture
{
    [self resignAllTextFiled];
}


- (void)resignAllTextFiled
{
    [_phoneTextField.textField resignFirstResponder];
    //[_macAddressCodeTextField.textField resignFirstResponder];
    [_passwordTextField.textField resignFirstResponder];
}



- (void)finishButtonPressed:(UIButton *)sender
{
    NSString *user = _phoneTextField.textField.text;
    //NSString *macAddress = _macAddressCodeTextField.textField.text;
    NSString *password = _passwordTextField.textField.text;
    if ([user checkEmpty])
    {
        [self promptMessage:@"请输入新的WIFI名称"];
        return;
    }
    
//    if ([macAddress checkEmpty]) {
//        [self promptMessage:@"MAC地址不能为空!"];
//        return;
//    }
    
    if ([password checkEmpty]) {
        [self promptMessage:@"密码不能为空!"];
        return;
    }else if (password.length < 8){
        [self promptMessage:@"密码不能少于8位!"];
        return;
    }
    
    WIFIModel *wifi = [UserSingleton shareInstance].userModel.wifi;
    //self.phoneTextField.textField.text = wifi.ssid;
    //self.macAddressCodeTextField.textField.text = wifi.device_id;
    [self findBackPasswordRequestWithSSID:user macAddress:wifi.device_id password:password];
}




- (void)findBackPasswordRequestWithSSID:(NSString *)user
                             macAddress:(NSString *)macAddress
                               password:(NSString *)password
{
    self.activity = [self startActivityWithTitle:@"正在修改..."];
    WEAKSELF
    [self.wifiHelper settingWIFIInfoWithSSID:user password:password deviceID:macAddress success:^(BOOL succ, id responseObject, NSString *msg) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        if (responseObject) {
            NSString *set = responseObject[@"set"];
            if ([set isEqualToString:@"success"]) {
                WIFIModel *wifi  =   [UserSingleton shareInstance].userModel.wifi;
                wifi.ssid = user;
                [UserSingleton shareInstance].userModel.wifi = wifi;
                [[UserSingleton shareInstance] saveData];
                
                [strongSelf stopActivityWithActivity:strongSelf.activity title:@"修改成功!"];
                [strongSelf dispatchTimerWithTime:2.0 block:^{
                    [strongSelf.navigationController popViewControllerAnimated:YES];
                }];
            }
            else{
                [strongSelf stopActivityWithActivity:strongSelf.activity title:@"修改失败!"];
            }
        }
        else
        {
            [strongSelf stopActivityWithActivity:strongSelf.activity title:@"修改失败!"];
        }
    } failure:^(NSError *error) {
        NSString *failedReason = [error localizedDescription];
        [self stopActivityWithActivity:self.activity title:failedReason];
    }];

}



@end
